package cn.leonis.leetcode;

import java.util.List;

/**
 * @Author Tobi
 * @Date 2022-07-01 下午3:24
 * leetcode852 在山峰数组里面查找峰顶的索引
 */
public class PeakIndexMountainArraySolution {
    public static void main(String[] args) {
        int[] arr = new int[]{0, 1, 3, 5, 6, 7, 6, 4, 2, 0};
        System.out.println(peakIndex(arr));
    }

    public static int peakIndex(int[] arr) {
        int length = arr.length;
        int left = 1, right = length - 2, answer = 0;
        while (left <= right) {
            int mid = left + ((right - left) >> 2);
            if (arr[mid] < arr[mid + 1]) {
                left = mid + 1;
            } else if (arr[mid] > arr[mid + 1]) {
                answer = mid;
                right = mid - 1;
            }
        }
        return answer;
    }
}
